#include "stdafx.h"
#include "FactionManager.h"

FactionManager::FactionManager()
{
}
FactionManager::~FactionManager()
{

}
Faction* FactionManager::Add(FactionId id,FactionInfo& info)
{
	Faction* pFaction = manager<Faction,FactionId>::Add(id);
	if (pFaction == NULL)
	{
		return NULL;
	}
	pFaction->SetFactionInfo(info);
	m_vectorRank.push_back(&(pFaction->GetFactionInfo()));
	m_nFactionVersion++;
	return pFaction;
}
bool FactionManager::Remove(FactionId id)
{
	Faction* pFaction = manager<Faction,FactionId>::Get(id);
	if (pFaction != NULL)
	{
		std::vector<FactionInfo*>::iterator iterVector = std::find(m_vectorRank.begin(),m_vectorRank.end(),&(pFaction->GetFactionInfo()));
		if (iterVector != m_vectorRank.end())
		{
			m_vectorRank.erase(iterVector);
			m_nFactionVersion++;
		}
	}
	return manager<Faction,FactionId>::Remove(id);
}
void FactionManager::GetFactionFromDataServer()
{
	LOGGER_INFO("GetFactionFromDataServer");

	PacketEncoder encoder;
	encoder.AddItem(Int32(0));
	encoder.AddItem(Int32(1800));
	SendPacketToData(encoder);
}
Faction* FactionManager::CreateFromFactionInfo(FactionInfo& info)
{
	Int32 nFactionId = info.m_nFactionId <= 0 ? GenerateIdInt32() : info.m_nFactionId;
	return Add(nFactionId,info);
}
bool FactionManager::HasFaction(std::string name)
{
	std::vector<FactionInfo*>::iterator iter = m_vectorRank.begin();
	std::vector<FactionInfo*>::iterator iterend = m_vectorRank.end();
	for (; iter != iterend; iter++)
	{
		if(std::string((*iter)->m_szFactionName) == name)
			return true;
	}
	return false;
}